package com.ruoyi.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.work.domain.TbLocationTrain;
import com.ruoyi.work.domain.vo.LocationTrainVo;
import com.ruoyi.work.service.ITbLocationTrainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 车辆位置Controller
 *
 * @author szm
 * @date 2025-09-28
 */
@Api(tags = "车辆位置接口")
@RestController
@RequestMapping("/work/TbLocationTrain")
public class TbLocationTrainController extends BaseController {
    @Autowired
    private ITbLocationTrainService tbLocationTrainService;

    /**
     * 查询车辆位置列表
     */
    @ApiOperation("查询车辆位置列表")
    @GetMapping("/list")
    public TableDataInfo list(TbLocationTrain entity, HttpServletRequest req) {
        startPage();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        wrapper.orderByAsc("id");
        List<TbLocationTrain> list = tbLocationTrainService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出车辆位置列表
     */
    @ApiOperation("导出车辆位置列表")
    @Log(title = "车辆位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbLocationTrain entity) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<TbLocationTrain> list = tbLocationTrainService.list(wrapper);
        ExcelUtil<TbLocationTrain> util = new ExcelUtil<TbLocationTrain>(TbLocationTrain. class);
        util.exportExcel(response, list, "车辆位置数据");
    }

    /**
     * 获取车辆位置详细信息
     */
    @ApiOperation("获取车辆位置详细信息")
    @GetMapping(value = "/getById")
    public AjaxResult getById(@RequestParam("id") String id) {
        return success(tbLocationTrainService.getById(id));
    }

    /**
     * 新增车辆位置
     */
    @ApiOperation("新增/修改车辆位置")
    @Log(title = "车辆位置", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody TbLocationTrain entity) {
        return toAjax(tbLocationTrainService.saveOrUpdate(entity));
    }

    /**
     * 删除车辆位置
     */
    @ApiOperation("删除车辆位置")
    @Log(title = "车辆位置", businessType = BusinessType.DELETE)
    @PostMapping("/delIds")
    public AjaxResult remove(@RequestParam("ids") String ids) {
        return toAjax(tbLocationTrainService.removeById(ids));
    }

    @PostMapping("/syncUpdate")
    public AjaxResult syncUpdate() {
        return toAjax(tbLocationTrainService.syncUpdate());
    }


    @ApiOperation("获取救援车辆行走轨迹")
    @GetMapping(value = "/getTrackList")
    public AjaxResult getTrackList() {
        Map<String, List<LocationTrainVo>> trackList = tbLocationTrainService.getTrackList();
        return success(trackList);
    }
}
